<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title + '员工'"
      :width="modalWidth"
      @ok="handleOk"
      @cancel="handleClose"
      :destroyOnClose="true"
      :visible="visible"
      :confirm-loading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="ruleForm" :model="model" :rules="rules">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="员工姓名"
                prop="name"
                hasFeedback
              >
                <a-input
                  placeholder="请输入员工姓名"
                  v-model.trim="model.name"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="用户名"
                prop="userName"
                hasFeedback
              >
                <a-input
                  :disabled="title === '编辑' ? true : false"
                  placeholder="请输入用户名"
                  v-model.trim="model.userName"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="邮箱"
                prop="email"
                hasFeedback
              >
                <a-input placeholder="请输入邮箱" v-model.trim="model.email" />
              </a-form-model-item>
            </a-col>

            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="员工编号"
                prop="employeeNumber"
                hasFeedback
              >
                <a-input
                  :disabled="true"
                  placeholder="请输入员工编号"
                  type="number"
                  v-model.trim="model.employeeNumber"
                />
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="部门"
                prop="departmentId"
                hasFeedback
              >
                <a-tree-select
                  v-model.trim="model.departmentId"
                  style="width: 100%"
                  :dropdown-style="{
                    maxHeight: '400px',
                    overflow: 'auto',
                  }"
                  :tree-data="treeData"
                  :replaceFields="replaceFields"
                  placeholder="请选择部门"
                  tree-default-expand-all
                ></a-tree-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="角色"
                prop="roleId"
                hasFeedback
              >
                <a-select v-model.trim="model.roleId" placeholder="请选择角色">
                  <a-select-option
                    v-for="item in RoleOption"
                    :key="item.id"
                    :value="item.id"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>

            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="手机号"
                prop="phone"
                hasFeedback
              >
                <a-input
                  placeholder="请输入手机号"
                  v-model.trim="model.phone"
                  style="width:100%"
                  :maxLength="11"
                />
              </a-form-model-item>
            </a-col>

            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="性别"
                prop="sex"
                hasFeedback
              >
                <a-select v-model.trim="model.sex" placeholder="请选择性别">
                  <a-select-option
                    v-for="item in genderOption"
                    :key="item.value"
                    >{{ item.label }}</a-select-option
                  >
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="职务"
                prop="position"
                hasFeedback
              >
                <a-input
                  placeholder="请输入职务"
                  v-model.trim="model.position"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="所学专业"
                prop="major"
                hasFeedback
              >
                <a-input
                  placeholder="请输入所学专业"
                  v-model.trim="model.major"
                />
              </a-form-model-item>
            </a-col>

            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="籍贯"
                prop="nativePlace"
                hasFeedback
              >
                <a-input
                  placeholder="请输入籍贯"
                  v-model.trim="model.nativePlace"
                />
              </a-form-model-item>
            </a-col>

            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="入职日期"
                prop="hiredate"
                hasFeedback
              >
                <a-date-picker
                  style="width:100%"
                  :showToday="false"
                  :disabled-date="disabledDate"
                  placeholder="请选择入职日期"
                  v-model.trim="model.hiredate"
                  :locale="locale"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <!-- line-5 -->
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="教育程度"
                prop="education"
                hasFeedback
              >
                <a-input
                  placeholder="请输入教育程度"
                  v-model.trim="model.education"
                />
              </a-form-model-item>
            </a-col>

            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="出生日期"
                prop="birthday"
                hasFeedback
              >
                <a-date-picker
                  :showToday="false"
                  :disabled-date="disabledDate"
                  v-model.trim="model.birthday"
                  style="width:100%"
                  placeholder="请选择出生日期"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="民族"
                prop="nation"
                hasFeedback
              >
                <a-input placeholder="请输入民族" v-model.trim="model.nation" />
              </a-form-model-item>
            </a-col>

            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="备注"
                prop="remark"
                hasFeedback
              >
                <a-input placeholder="请输入备注" v-model.trim="model.remark" />
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="现住地址"
                prop="currentAddress"
                hasFeedback
              >
                <a-input
                  placeholder="请输入现住地址"
                  v-model.trim="model.currentAddress"
                />
              </a-form-model-item>
            </a-col>

            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="职称"
                prop="professionalTitle"
                hasFeedback
              >
                <a-input
                  placeholder="请输入职称"
                  v-model.trim="model.professionalTitle"
                />
              </a-form-model-item>
            </a-col>

            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="家庭电话"
                prop="homeTel"
                hasFeedback
              >
                <a-input
                  placeholder="请输入家庭电话"
                  v-model.trim="model.homeTel"
                />
              </a-form-model-item>
            </a-col>

            <a-col :span="6">
              <a-form-model-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="上传图片"
                prop="pic"
                hasFeedback
              >
                <ImageUpload v-model.trim="model.pic"></ImageUpload>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";
import ImageUpload from "@/components/upload/ImageUpload";
import { validatorPhone, isChinaName, isEngName } from "@/unit/rule";
import moment from "moment";
import zhCN from "ant-design-vue/lib/locale-provider/zh_CN";
export default {
  components: { ImageUpload },
  data() {
    return {
      locale: zhCN,
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 1677, // 抽屉宽度
      model: {
        name: "",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 },
      },
      confirmLoading: false,
      // 性别数据源
      genderOption: [
        { label: "男", value: 1 },
        { label: "女", value: 0 },
      ],
      treeData: [],
      RoleOption: [],
      replaceFields: {
        title: "departmentName",
        key: "id",
        value: "id",
        children: "childList",
      },
    };
  },
  computed: {
    rules() {
      return {
        name: [
          { required: true, message: "请输入员工姓名", trigger: "change" },
          { validator: isChinaName, trigger: "change" },
        ],
        userName: [
          { required: true, message: "请输入用户名", trigger: "change" },
          { validator: isEngName, trigger: "change" },
        ],
        employeeNumber: [
          { required: true, message: "请输入员工编号", trigger: "change" },
        ],
        email: [{ required: true, message: "请输入邮箱", trigger: "change" }],
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          { validator: validatorPhone, trigger: "change" },
        ],
        departmentId: [
          { required: true, message: "请选择部门", trigger: "change" },
        ],
        roleId: [{ required: true, message: "请选择角色", trigger: "change" }],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
      };
    },
  },
  methods: {
    moment,
    disabledDate(current) {
      const yearStart = moment().startOf("month");
      const yearEnd = moment().endOf("day");
      return current >= yearEnd;
    },
    changeRadio(name, value) {
      this.model[name] = value;
    },
    async getRole() {
      const res = await api.RoleFindAll();
      this.RoleOption = res.data;
    },
    async getTree() {
      const res = await api.DepartmentTree();
      this.treeData = res.data;
    },
    // 新增
    modalAdd() {
      this.modalEdit(this.model);
    },
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.getRole();
      this.visible = true;
      this.model = Object.assign({}, record);
      if (this.model.roleId) {
        this.model.roleId = Number(this.model.roleId);
      }
      if (!record.id) {
        this.getUserEmployeeNumber();
      }
      this.getTree();
    },
    // 获取用户编号
    async getUserEmployeeNumber() {
      const res = await api.UserEmployeeNumber();
      this.$set(this.model, "employeeNumber", res.data);
    },
    async submitVulFun() {
      this.confirmLoading = true;
      let res;
      if (!this.model.id) {
        res = await api.UserAdd(this.model);
      } else {
        res = await api.UserEdit(this.model);
      }
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        return;
      }
      this.confirmLoading = false;
      this.$message.success(res.message);
      this.$emit("ok");
      this.model = Object.assign({});
      this.visible = false;
    },
    comparedate(date1, date2) {
      let oDate1 = new Date(date1);
      let oDate2 = new Date(date2);
      if (oDate1.getTime() == oDate2.getTime()) {
        return true;
      } else if (oDate1.getTime() > oDate2.getTime()) {
        return true;
      } else {
        return false;
      }
    },

    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (that.model.hiredate && that.model.birthday) {
            if (that.model.hiredate > that.model.birthday) {
            } else {
              that.$message.error("入职日期不能小于出生日期");
              return;
            }
          }
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.model = Object.assign({});
      this.$refs.ruleForm.resetFields();
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 1677;
      }
    },
  },
};
</script>

<style lang="less" scoped></style>
